package com.example.toucheventhandle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;

import android.content.Context;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MyButton button=new MyButton(this);
        button.setText("TouchEvent按钮");
        button.setTextSize(20);
        button.setAllCaps(false);
        setContentView(button);
    }

    private class MyButton extends AppCompatButton {
        public MyButton(Context context) {
            super(context);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            switch(event.getAction()){
                case MotionEvent.ACTION_DOWN:
                    Toast.makeText(MainActivity.this, "按钮被按下", Toast.LENGTH_SHORT).show();
                    break;
                case MotionEvent.ACTION_UP:
                    Toast.makeText(MainActivity.this, "按钮被释放", Toast.LENGTH_SHORT).show();
                    break;
                case MotionEvent.ACTION_MOVE:
                    Toast.makeText(MainActivity.this, "在按钮上进行移动", Toast.LENGTH_SHORT).show();
                    break;
            }
            return super.onTouchEvent(event);
        }
    }
}
